var app = getApp();
Page({
  data: {
    packages: [],
    page: 1,
    pageSize: 10,
    loading: false,
    noMoreData: false,
    searchKey: '',
    selectedHospital: '',
    selectedPeople: '',
    sortText: '价格排序',
    sortIcon: 'up',
	hospitals:[],
	peopleTypes:[],
  },

  onLoad() {
	  // 页面加载时的逻辑
	  let self = this;
	  let aid = '';
	    if(!aid){
	  	  aid=wx.getStorageSync('VP_AID');
	    }	    
	    app.util.getUserInfo(function (response) {
	    	console.log("==获取当前用户信息1==");
	    	console.log(response);
	    	self.setData({
	    		userInfo: response
	    	});
	    	console.log("==应用初始化：应用配置/应用中用户信息/当前城市信息==");
	    	
	    	setTimeout(function(){
	    		app.mdInit(function (cfg,mine,area) {
	    			self.setData({
	    				cfg: cfg,
	    				mine:mine,
	    				area:area
	    			});
					
					setTimeout(function(){
						self.loadPackageList()						
						self.getsearchData()
					},500)
	    		},{
	    				aid:self.data.aid,
	    				fuid:self.data.fuid,
	    				stid:self.data.fuid
	    		},true);
	    	},500)
	    });    
  },

  // 加载套餐列表
  loadPackageList() {
    if (this.data.loading || this.data.noMoreData) return;

    this.setData({ loading: true });
	let self = this
    // 接口请求
    app.util.request({
      url: 'Physical/packagelists',
      data: {
        page: self.data.page,
        pageSize: self.data.pageSize,
        keyword: self.data.searchKey,
        hospital: self.data.selectedHospital,
        peopleType: self.data.selectedPeople
      },
      success: (res) => {
        if (res.data.code === 1) {
          const newList = res.data.data.list;
          const total = res.data.data.total;
          console.log(newList)
          self.setData({
            packages: self.data.page === 1 ? newList : [...self.data.packages, ...newList],
            noMoreData: self.data.packages.length >= total,
            page: self.data.page + 1
          });
        } else {
          wx.showToast({
            title: '加载失败',
            icon: 'none'
          });
        }
      },
      complete: () => {
        self.setData({ loading: false });
      }
    });
  },
  getsearchData(){
	  let self = this
	  // 接口请求
	  app.util.request({
	    url: 'Physical/getsearchdata',
	    data: {},
	    success: (res) => {
	      if (res.data.code === 1) {
	        this.setData({
				hospitals:res.data.data.hospital,
				peopleTypes:res.data.data.peopletype
			})
	      } else {
	        wx.showToast({
	          title: '加载失败',
	          icon: 'none'
	        });
	      }
	    }
	  });
  },
  // 搜索输入
  onSearchInput(e) {
    this.setData({
      searchKey: e.detail.value,
      page: 1,
      noMoreData: false
    }, () => {
      this.loadPackageList();
    });
  },

  // 显示医院筛选
  showHospitalFilter() {
    // 实际项目中可弹出筛选弹窗
    wx.showActionSheet({
      itemList: this.data.hospitals,
      success: (res) => {
        const hospitals = this.data.hospitals;
        this.setData({
          selectedHospital: hospitals[res.tapIndex],
          page: 1,
          noMoreData: false
        }, () => {
          this.loadPackageList();
        });
      }
    });
  },

  // 显示人群筛选
  showPeopleFilter() {
    wx.showActionSheet({
      itemList: this.data.peopleTypes,
      success: (res) => {
        const peopleTypes = this.data.peopleTypes;
        this.setData({
          selectedPeople: peopleTypes[res.tapIndex],
          page: 1,
          noMoreData: false
        }, () => {
          this.loadPackageList();
        });
      }
    });
  },

  // 排序切换
  sortPackages() {
    const sortIcon = this.data.sortIcon === 'up' ? 'down' : 'up';
    this.setData({
      sortIcon,
      page: 1,
      noMoreData: false
    }, () => {
      this.loadPackageList();
    });
  },

  // 加载更多
  loadMore() {
    this.loadPackageList();
  },

  // 前往详情页
  goToDetail(e) {
    const packageId = e.currentTarget.dataset.id;
	console.log(packageId)
    wx.navigateTo({
      url: `/vp_pz/pages/physicalExam/packageDetail/packageDetail?packageId=${packageId}`
    });
  }
});